package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.domain.customer.CustomerDO;
import com.qk.dto.customer.CustomerDTO;
import com.qk.dto.customer.CustomerListDTO;
import com.qk.entity.Customer;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Override
    public PageResult<CustomerDO> listCustomer(CustomerListDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<CustomerDO> list =  this.baseMapper.listCustomer(dto);
        Page<CustomerDO> page = (Page<CustomerDO>) list;
        return PageResult.<CustomerDO>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getResult())
                .build();
    }

    @Override
    public void addCustomer(CustomerDTO dto) {
        Customer customer = BeanUtil.copyProperties(dto, Customer.class);
        customer.setUpdateTime(LocalDateTime.now());
        customer.setCreateTime(LocalDateTime.now());
        this.baseMapper.insert(  customer);
    }

    @Override
    public Customer getCustomerById(Integer id) {
        return this.baseMapper.selectById(id);
    }


}
